/*给相应的链接添加地址*/
var allBuildQALink = function(record) {
    //WebSite.debug(record.status);
    var status=record.status;
    var pk_sool_question=record.pk_sool_question;
    var pk_answer_user = record.pk_answer_sool_user;

    if (status == WebSite.askerWaitToAnswerStatus) {
        //提问者待解答
        return WebSite.AskerUnansweredPage.url + "&pk_question="+pk_sool_question;
    } else if (status == WebSite.askerWaitToHandle) {
        //提问者待处理
        return WebSite.AskerHandlingPage.url + "&pk_question="+pk_sool_question+"&pk_answer_user="+pk_answer_user;
    } else {
        //提问者已解决
        return WebSite.AskerSolvedPage.url + "&pk_question="+pk_sool_question;
    }
};

/**
 * 搜索按鈕調取的
 * @param pageno
 */
var getSearchContext = function(pageno) {
    var model= $("#model").val();
    var vision=$("#vision").val();
    var type=$("#type").val();
    if(model ==null){
        model='';
    }
    if(vision ==null){
        vision='';
    }
    if(type ==null){
        type='';
    }
    if (pageno == null) {
        pageno = 1;
    }
    //在其他页面点击搜索按钮

    //根据keyword自动搜索一次,获得搜索结果
    $.ajax({
        type: "POST",
        data: {
            "keyword": $("#kw").val().trim(),
            "pageno": pageno,
            "model": model,
            "vision":vision,
            "type":type,
            "methodname": "getSearchContext"
        },
        url: WebSite.SearchResultPage.searchResult_list,
        dataType: "json",
        success: function (searchData) {
            var search_data = searchData.resultData;
            for (var i = 0, len = search_data.length; i < len; i++) {
                var record = search_data[i];
                record.sd_link = searchDetailPath(record);
            }
            var pagemess = searchData.pagemess;
            $('#pagemes').html(pagemess);
            var myTemplate = Handlebars.compile($("#template").html());
            $("#contain-all-left").html(myTemplate(search_data));
        },
        error: function () {
            alert("问题的详细信息未获取到");
        }
    });

    //精华问题
    similarQuestion();
}

//在搜索页点击搜素
var click_button = function() {

    $("#searchButton").click(function(){
        getSearchContext();
    });
};

var searchDetailPath = function(record){
    //问题的状态 1待解答 2待处理 3已解决
    var status = record.status;
    //问题是不是我提出的 0不是 1是
    var isMine = record.ismyselft;

    var ismyanswer=record.ismyanswer;

    if (isMine == 1 && status == 1){
        return WebSite.AskerUnansweredPage.url + "&pk_question=" + record.pk_sool_question;
    }else if (isMine == 1 && status == 2){
        return WebSite.AskerHandlingPage.url + "&pk_question=" + record.pk_sool_question+"&pk_answer_user="+record.pk_answer_sool_user;
    }else if (isMine == 1 && status == 3){
        return WebSite.AskerSolvedPage.url + "&pk_question=" + record.pk_sool_question;
    }else if (isMine == 0 && status == 1){//不是我提问的，也没有回答呢

        return WebSite.ReplyerUnansweredPage.url + "&pk_question=" + record.pk_sool_question;//回答页面
    }else if (isMine == 0 && status == 2){//这是不是我提问的，但是已经被人回答了
        //是我回答的，已经被追问   //是我回答的，沒有被追问
        if(ismyanswer == 1){
            //追答
            return WebSite.ReplayerHandlingPage.url + "&pk_question=" + record.pk_sool_question;//追答页面
        }else{
            //不是我回答的，----進來回答
            return WebSite.ReplyerUnansweredPage.url + "&pk_question=" + record.pk_sool_question;//回答页面

        }
    }else{
        return WebSite.AskerSolvedPage.url + "&pk_question=" + record.pk_sool_question;//已经解决的
    }
};


var essencePath = function(record){
    return WebSite.AskerSolvedPage.url + "&pk_question=" + record.pk_sool_question;
};

//改变时间格式
Handlebars.registerHelper("transdate", function(str,options) {
    return new Date(parseInt(str)).toLocaleString();
});


//问题模块
/*var get_question_module = function(){
    var models='';
    $.ajax({
        url: WebSite.AllQuestionPage.allQuestion_template,
        type: "post",
        dataType: "json",
        success: function (result) {
            if (result.success) {
                var data = result.resultData;
                var myTemplate = Handlebars.compile($("#allQuestion_questiontemplate").html());
                $("#questiontemplate").html(myTemplate(data));

                $(".tag-module-all").toggleClass("tag-selected");

                $(".tag-module-all").click(function(){
                    $(".tag-module").removeClass("tag-selected");
                    $(this).toggleClass("tag-selected");
                    searchContext();
                });

                $(".tag-module").click(function(){

                    $(".tag-module-all").removeClass("tag-selected");
                    $(this).toggleClass("tag-selected");
                    if ($(this).hasClass("tag-module-all")){
                        return;
                    }
                    var modules_arr  = [];

                    $(".tag-module.tag-selected").each(function(index, ele) {
                        modules_arr.push($(ele).attr("data-module"));
                    });

                    $("#model").val(modules_arr);

                    searchContext();
                });

            }
        },
        error: function () {
            alert("没有获得数据");
        }
    });
};*/

/*
//问题类型模块
var get_type_module = function(){
    alert(WebSite.AllQuestionPage.allQuestion_versiontemplate);
    $.ajax({
        url: WebSite.AllQuestionPage.allquestion_type,
        type: "post",
        dataType: "json",
        success: function (result) {
            if (result.success) {
                var data = result.resultData;
                var myTemplate = Handlebars.compile($("#allQuestion_typetemplate").html());
                $("#typetemplate").html(myTemplate(data));

                $(".tag-type-all").toggleClass("tag-selected");

                $(".tag-type-all").click(function(){
                    $(".tag-type").removeClass("tag-selected");
                    $(this).toggleClass("tag-selected");
                    searchContext();
                });

                $(".tag-type").click(function(){

                    $(".tag-type-all").removeClass("tag-selected");
                    $(this).toggleClass("tag-selected");
                    if ($(this).hasClass("tag-type-all")){
                        return;
                    }
                    var types_arr  = [];

                    $(".tag-type.tag-selected").each(function(index, ele) {
                        types_arr.push($(ele).attr("data-type"));
                    });

                    $("#type").val(types_arr);
                    //console.log(modules_arr);
                    searchContext();
                });

            }
        },
        error: function () {
            alert("没有获得数据");
        }
    });
};

//版本模块
var get_version_module = function(){
    $.ajax({
        url: WebSite.AllQuestionPage.allQuestion_versiontemplate,
        type: "post",
        dataType: "json",
        success: function (result) {
            if (result.success) {
                var data = result.resultData;
                var myTemplate_version = Handlebars.compile($("#allQuestion_versiontemplate").html());
                $("#versiontemplate").html(myTemplate_version(data));

                $(".tag-version-all").toggleClass("tag-selected");

                $(".tag-version-all").click(function(){
                    $(".tag-version").removeClass("tag-selected");
                    $(this).toggleClass("tag-selected");
                    searchContext();
                });

                $(".tag-version").click(function(){
                    $(".tag-version-all").removeClass("tag-selected");
                    $(this).toggleClass("tag-selected");
                    if ($(this).hasClass("tag-version-all")){
                        return;
                    };
                    var versions_arr = [];
                    $(".tag-version.tag-selected").each(function(index, ele) {
                        versions_arr.push($(ele).attr("data-version"));
                    });
                    $("#vision").val(versions_arr);
                    //console.log(versions_arr);
                    searchContext();
                });

            }
        },
        error: function () {
            alert("没有获得数据");
        }
    });
};


//点击全部时发送请求的方法
var searchContext_all = function(pageno){
    if(pageno==null){
        pageno=1;
    }
    var versions_arr = [];
    var modules_arr  = [];
    var types_arr     = [];
    $(".tag-module-all.tag-selected").each(function(index, ele) {
        modules_arr.push($(ele).attr("data-module"));
    });
    $(".tag-type.tag-selected").each(function(index, ele) {
        types_arr.push($(ele).attr("data-type"));
    });
    $(".tag-version.tag-selected").each(function(index, ele) {
        versions_arr.push($(ele).attr("data-version"));
    });

    var extParams = $.param({
        "vision": versions_arr,
        "type"  : types_arr,
        "model" : modules_arr
    }, true);
alert(WebSite.AllQuestionPage.search + "&" + extParams);
    $.ajax({
        url: WebSite.SearchResultPage.searchResult_list + "&" + extParams,
        type: "post",
        data: {
            "keyword": $("#kw").val(),
            "pageno":  pageno,
            "methodname":"searchContext_all"
        },
        dataType: "json",
        success: function (result) {
            if (result.success) {
                var data = result.resultData;
                for (var i = 0, len = data.length; i < len; i++) {
                    var record = data[i];
                    record.ALLQ_link = searchDetailPath(record);
                }
                var pagemess=result.pagemess;
                $('#pagemes').html(pagemess);
                var myTemplate = Handlebars.compile($("#template").html());
                $("#contain-all-left").html(myTemplate(data));
            }
        },
        error: function () {
            alert("没有获得22数据");
        }
    });
};

//点击版本，问题类型，版本时调的方法（不含点击各部分的全部）
var searchContext = function(pageno){
    if(pageno==null){
        pageno=1;
    }

    if ($(".tag-module.tag-selected").size() < 1) {
        $(".tag-module-all").toggleClass("tag-selected");
    }

    if ($(".tag-type.tag-selected").size() < 1) {
        $(".tag-type-all").toggleClass("tag-selected");
    }

    if ($(".tag-version.tag-selected").size() < 1) {
        $(".tag-version-all").toggleClass("tag-selected");
    }

    if ($(".tag-module.tag-module-all.tag-selected").size() > 0) {
        searchContext_all();
        return;
    }

    var versions_arr = [];
    var modules_arr  = [];
    var types_arr     = [];


    $(".tag-module.tag-selected").each(function(index, ele) {
        modules_arr.push($(ele).attr("data-module"));
    });
    $(".tag-type.tag-selected").each(function(index, ele) {
        types_arr.push($(ele).attr("data-type"));
    });
    $(".tag-version.tag-selected").each(function(index, ele) {
        versions_arr.push($(ele).attr("data-version"));
    });


    var extParams = $.param({
        "vision": versions_arr,
        "type"  : types_arr,
        "model" : modules_arr
    }, true);

    /!*console.log(extParams);*!/

    alert(WebSite.AllQuestionPage.search + "&" + extParams);
    $.ajax({
        url: WebSite.SearchResultPage.searchResult_list + "&" + extParams,
        type: "post",
        data: {
            "keyword": $("#kw").val(),
            "pageno":  pageno,
            "methodname":"searchContext"

        },
        dataType: "json",
        success: function (result) {
            if (result.success) {
                var data = result.resultData;
                for (var i = 0, len = data.length; i < len; i++) {
                    var record = data[i];
                    record.ALLQ_link = searchDetailPath(record);
                }
                var pagemess=result.pagemess;
                $('#pagemes').html(pagemess);
                var myTemplate = Handlebars.compile($("#template").html());
                $("#contain-all-left").html(myTemplate(data));
            }
        },
        error: function () {
            alert("没有获得数据");
        }
    });
}

var registerHelper_status = function () {
}


Handlebars.registerHelper("compare",function(v1,options){
    if(v1 == 3){
        return options.fn(this);
    }
    else{
        return options.inverse(this);
    }
});
*/

/*精华问题加载*/
var similarQuestion = function(){
    $.ajax({
        url:WebSite.SearchResultPage.essencequestion_list,
        type:"POST",
        data:{"count":10},
        dataType:"json",
        success:function(resultData){
            var data = resultData.resultData;
            for (var i = 0, len = data.length; i < len; i++) {
                var record = data[i];
                var pk_sool_question = record.pk_sool_question;
                record.qa_link = WebSite.AskerSolvedPage.url + "&pk_question="+pk_sool_question;
            }
            var myTemplate = Handlebars.compile($("#li_template").html());
            $("#xsQuestionList").html(myTemplate(data));
        },
        error:function(){
            alert("精华问题未加载");
        }
    });

};

